<template>
	<a-card :bordered="false" v-if="indexShow">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">

			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'reportDefault'">
					<a-tag color="red">{{ record.reportDefault == '0' ? ' ' : '默认' }}</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="openDesign(record)" v-if="hasPerm('supplierSettingReportsEdit')">修改</a>
						<a-divider type="vertical" v-if="hasPerm(['supplierSettingReportsEdit', 'supplierSettingReportsCopy'], 'and')" />
						<a @click="copyRecord(record)" v-if="hasPerm('supplierSettingReportsCopy')">复制</a>
						<a-divider
							type="vertical"
							v-show="record.reportDefault == 0 ? true : false"
							v-if="hasPerm(['supplierSettingReportsCopy', 'supplierSettingReportsDelete'], 'and')"
						/>
						<a-popconfirm title="确定要删除吗？" @confirm="deletePublicReports(record)">
							<a-button
								type="link"
								danger
								size="small"
								v-show="record.reportDefault == 0 ? true : false"
								v-if="hasPerm('supplierSettingReportsDelete')"
							>删除</a-button
							>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<design v-else ref="designRef" @successful="tableRef.refresh(true)" @closed="closeDesign()" />
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="supplierSettingReports">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import design from './design.vue'
	import publicReportsApi from '@/api/supplier/setting/publicReportsApi'
	const tableRef = ref()
	const formRef = ref()
	const designRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const indexShow = ref(true)
	const columns = [
		{
			title: '报表格式名称',
			dataIndex: 'reportName'
		},
		{
			title: '默认格式',
			dataIndex: 'reportDefault'
		},
		{
			title: '报表类型',
			dataIndex: 'typeName'
		},

	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['supplierSettingReportsEdit', 'supplierSettingReportsDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 250
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		return publicReportsApi.publicReportsPage(parameter).then((data) => {
			return data
		})
	}
	//打开设计界面
	const openDesign = (record) => {
		indexShow.value = false
		nextTick(() => {
			designRef.value.onOpen(record)
		})
	}
	//关闭设计界面
	const closeDesign = () => {
		indexShow.value = true
	}
	const copyRecord = (record) => {
		let params = {
			reportName: record.reportName + '复制',
			reportType: record.reportType,
			reportDefault: 0,
			reportContent: record.reportContent,
			reportDataTools: record.reportDataTools
		}
		publicReportsApi.publicReportsSubmitForm(params, params.id).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 删除
	const deletePublicReports = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		publicReportsApi.publicReportsDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchPublicReports = (params) => {
		publicReportsApi.publicReportsDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
